package au.edu.uq.itee.engg2800.team19;

import java.io.IOException;
import java.io.InputStream;


public class DataReceiver extends Thread  {

    InputStream in;
    RS232Protocol protocol;
    byte[] buffer = new byte[1024];
	int freqCounter =1;
    int tail = 0;
    //private String message;
    private FrequenciesReceived freqs;

    public DataReceiver(InputStream in,FrequenciesReceived freq) {
        this.in = in;
        this.freqs=freq;
        protocol= new JHyperProtocol(freqs);
    }

    public void run() {
        try {
            int b;
            while(true) {

                // if stream is not bound in.read() method returns -1
                while((b = in.read()) != -1) {
                    protocol.onReceive((byte) b);

                }
                protocol.onStreamClosed();

                // wait 10ms when stream is broken and check again
                sleep(10);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}